﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TableLayoutDetectorOutput:ResultBase
    {
        #region props
        private string _TableLayoutOutputFilePath;
        /// <summary>
        /// csv file in the following format
        /// 0   filename
        /// 1   page number
        /// 2   colname
        /// 3   rectangle_x
        /// 4   y
        /// 5   width
        /// 6   height
        /// </summary>
        public string TableLayoutOutputFilePath
        {
            get
            {
                return this._TableLayoutOutputFilePath;
            }
            set
            {
                this._TableLayoutOutputFilePath = value;
            }
        }

        private int _FilesProcessed;
        /// <summary></summary>
        public int FilesProcessed
        {
            get
            {
                return this._FilesProcessed;
            }
            set
            {
                this._FilesProcessed = value;
            }
        }

        private int _FileCountWithTableFound;
        /// <summary></summary>
        public int FileCountWithTableFound
        {
            get
            {
                return this._FileCountWithTableFound;
            }
            set
            {
                this._FileCountWithTableFound = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TableLayoutDetectorOutput():base(Guid.Empty,Guid.Empty)
        {
            this._TableLayoutOutputFilePath = string.Empty;
            this._FileCountWithTableFound = 0;
            this._FilesProcessed = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="outputFilePath"></param>
        /// <param name="filesProcessed"></param>
        /// <param name="filesWithTblData"></param>
        public TableLayoutDetectorOutput(Guid componentID, Guid jobTicketID,
            string outputFilePath,
            int filesProcessed,
            int filesWithTblData) 
            : base(componentID, jobTicketID)
        {
            this._TableLayoutOutputFilePath = outputFilePath;
            this._FileCountWithTableFound = filesWithTblData;
            this._FilesProcessed = filesProcessed;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TableLayoutDetectorOutput output = new TableLayoutDetectorOutput();

                output.TableLayoutOutputFilePath = XmlDataUtil.GetAttributeValue(xNode, "OutputFilePath", "");
                output.FileCountWithTableFound =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "FilesWithTableData", "0"));
                output.FilesProcessed = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "FilesProcessed", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"OutputFilePath",this.TableLayoutOutputFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"FilesWithTableData",this.FileCountWithTableFound.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"FilesProcessed",this.FilesProcessed.ToString());

            return xNode;
        }
    }
}
